package com.zyw.service.order.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.OrderClient;
import com.zyw.from.OrderFrom;
import com.zyw.mapper.order.OrderClientMapper;
import com.zyw.service.order.OrderClientService;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class OrderClientServiceImpl implements OrderClientService {

    @Resource
    public OrderClientMapper orderClientMapper;

    @Override
    public LayUITableVO<List<OrderClient>> findPage(Map<String, Object> map) {
        OrderFrom from = (OrderFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(OrderClient.class);
        Example.Criteria c = example.createCriteria();


        if (! "".equals(from.getClientName()) && null!=from.getClientName()){
            c.andLike("name","%"+from.getClientName()+"%");
        }

        List<OrderClient> list = orderClientMapper.selectByExample(example);
        for (OrderClient orderClient : list) {
            if (orderClient.getStatus() == 0){
                orderClient.setStatusString("无效客户");
            }else {
                orderClient.setStatusString("有效客户");
            }
        }

        PageInfo<OrderClient> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<OrderClient>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public OrderClient selectById(String id) {
        return orderClientMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(OrderClient orderClient) {

        orderClientMapper.updateByPrimaryKeySelective(orderClient);

    }

    @Override
    @Transactional
    public void save(OrderClient orderClient) {

        orderClientMapper.insertSelective(orderClient);
    }

    @Override
    @Transactional
    public void delelteById(String id) {

        orderClientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<OrderClient> findAllIdAndName() {
        return orderClientMapper.findAllIdAndName();
    }
}
